package com.avic.rong.utils;

import com.avic.rong.publishVideo.videoEdit.Data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Creater: Created by wangyz on 2017/3/13.
 * Email: 936804097@qq.com
 */

public class DateUtils {

    public static final String FORMAT_SHORT = "yyyy-MM-dd";
    public static final String FORMAT_SHORT_CN = "yyyy年MM月dd日";

    public static String getNowDateTime(){
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
        String format = format2.format(new Date());
        String[] split = format.split("-");
        StringBuffer sb = new StringBuffer();
        sb.append(split[0]+"年"+split[1]+"月"+split[2]+"日");
        return sb.toString();
    }



    public static String getBeforeDateShort(int days){
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
        Date dBefore1 = new Date();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(dBefore1);
        calendar1.add(Calendar.DAY_OF_MONTH,-days);//往上推一天  30推三十天  365推一年
        Date time1 = calendar1.getTime();
        String format = sdf1.format(time1);
        return format;
    }

    public static String getBeforeDateTime(int i){
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date dBefore1 = new Date();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(dBefore1);
        calendar1.add(Calendar.DAY_OF_MONTH,-i);//往上推一天  30推三十天  365推一年
        Date time1 = calendar1.getTime();
        String format = sdf1.format(time1);
        String[] split = format.split("-");
        StringBuffer sb = new StringBuffer();
        sb.append(split[0]+"年"+split[1]+"月"+split[2]+"日");
        return sb.toString();
    }

    public static String getFormatDateTime(String time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(date);
    }

    public static String getFormatDateTime(long time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(date);
    }

    public static String getFormatShort(long time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }

    public static String getFormatShort(long time,String form) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat(form);
        return formatter.format(date);
    }

    /**
     *
     * @param time  2017-08-21
     * @return  2017.05.01
     */
    public static String getFormatShort(String time) {
        Date date = new Date(time);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
        return formatter.format(date);
    }

    private static long lastClickTime;

    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 700) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    /**
     * 获取精确到秒的时间戳
     * @return
     */
    public static int getSecondTimestamp(Date date){
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime());
        int length = timestamp.length();
        if (length > 3) {
            return Integer.valueOf(timestamp.substring(0,length-3));
        } else {
            return 0;
        }
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }


    public static long dateToStamp(String s,String format) {
        try{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            Date date = simpleDateFormat.parse(s);
            return date.getTime();
        }catch (Exception e){
            return 0L;
        }
    }

    /**	 * 调用此方法输入所要转换的时间戳输入例如（1402733340）输出（"2014-06-14  16:09:00"）
     * *
     * * @param time
     * * @return
     * */
    public static String timedate(String time)
    {
        if (time==null){
            return "";
        }
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        @SuppressWarnings("unused")
        long lcc = Long.valueOf(time);
        int i = (int) lcc;
        String times = sdr.format(new Date(i * 1000L));
        return times; 	}


    /*
   * 获取两个事件戳的差，转成字符串//  例子：三小时20分钟前
   */
    public static String getDistanceTime(long  time1,long time2 ) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        long diff ;
        String flag;
        if(time1<time2) {
            diff = time2 - time1;
            flag="前";
        } else {
            diff = time1 - time2;
            flag="后";
        }
        day = diff / (24 * 60 * 60 * 1000);
        hour = (diff / (60 * 60 * 1000) - day * 24);
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
        if(day!=0)return day+"天"+flag;
        if(hour!=0)return hour+"小时"+flag;
        if(min!=0)return min+"分钟"+flag;
        return "刚刚";
    }


}
